package wh.ispct.util

import wh.ispct.Content
import wh.ispct.FileItem;
import wh.ispct.LinkContent
import wh.ispct.Menu

class ContentTagLib {
	static namespace = 'content'
	
	def weekdays = [1:"星期一",2:"星期二",3:"星期三",4:"星期四",5:"星期五",6:"星期六",7:"星期日"]
	
	def oldPageLink = {
		LinkContent oldPage = LinkContent.findByLinkText("旧版主页")
		out << "<a href='${oldPage?.linkUrl}' target='_blank'>旧版</a>"
	}
	
	def date = { attrs,body ->
		if(attrs.date){
			out << attrs.date.format("yyyy年MM月dd日")
		}else{
			Calendar c = Calendar.getInstance(Locale.CHINA)
			out << c.format("yyyy年MM月dd日")
			out << "(" + weekdays[c.get(Calendar.DAY_OF_WEEK)] + ")"
			out << " " + c.format("HH时mm分")
		}
	}
	
	def text  = {attrs, body ->
		if(attrs.menu){
			Menu menu = Menu.findByName(attrs.menu)
			if(menu){
				Content content = Content.findByMenu(menu)
				if(content)
					out << content.text
			}else
				out << "未找到内容"
		}else if(attrs.title){
			Content content = Content.findByTitle(title)
			if(content)
				out << content.text
			else
				out << "未找到内容"
		}
	}
	
	def imgSlider = {
		List<FileItem> images = FileItem.findAll("from FileItem as f where f.fileType='img' order by f.id desc",[max: 8])
		out << "<div id=\"KinSlideshow\" style=\"visibility: hidden;\">"
		if(images.size() > 0){
			images.each {
			
			out << """
					<a href="/inspector/content/show/${it.content?.id}" target="_blank"><img src="/inspector/fileItem/get/${it?.id}" 
						title="标题：${it?.content?.title } &#13;来源：${it.content?.source} &#13;作者:${it?.content?.author}&#13发布日期：${it?.content?.createTime?.format("yy年MM月dd日") }"
						"""
			  	if(it!=null&&it.content!=null&&it.content.title!=null)
					out<<"""		alt="${it?.content?.title?.size() > 20? it?.content?.title?.substring(0,20) + ".." : it?.content?.title }" 
							width="355" height="280" /> </a> """
				else
					out<<"""     alt="" width="355" height="280" /> </a>"""
			}
		}else{
		out << """
		<a href="javascript:void(0)" target="_blank"><img src="/inspector/images/gallery/nophoto.jpg" 
					alt="尚未体检图片新闻" 
					width="355" 
					height="280" /> </a>
"""
		}
		out << "</div>"
	}
	
	def pager = {attrs ->
		
		if(attrs.form){
			out << """
			<input id=max name="max" value="${params.max?params.max:25}"  type="hidden"/>
			<input id=offset name="offset" value="${params.offset?params.offset:0}" type="hidden"/>
"""
		}
		
		int total = attrs.total.toInteger()
		int max = attrs.max? attrs.max.toInteger() : 25
		int offset = attrs.offset? attrs.offset.toInteger() : 0
		int pageCount=Math.round(Math.ceil(total / max))
		int page = Math.round(Math.ceil((offset + 1)/ max))
		
		int prev = page > 1 ?  (page-2) * max  :  -1
		int next = page < pageCount ? (page) * max  :  -1
		int end = (pageCount - 1) * max
		//println "page:" + page + ",prev:" + prev + ",next:" + next
		
		def appendLink = {
			_offset,_text ->
			
			if(_offset >= 0){
				if(attrs.form){
					out << """<a href="javascript:postPage(${_offset});" target="_self"> ${_text}</a> """
				}else
          			out << """<a href="/inspector/menu/show/${params.id}?max=${max}&offset=${_offset}" target="_self"> ${_text}</a> """
			}else
				out << """<a href="javascript:void(0)" class="disabled">${_text} </a> """ 
		}
		
		out << """<div class="pager">"""
		appendLink((page < 2 ? -1: 0),"|<首页 ")  //<a href="/inspector/menu/show/${params.id}?max=${max}&offset=0" target="_self">|<首页 </a> """
		appendLink(prev,"<<上一页 ")  //<a href="/inspector/menu/show/${params.id}?max=${max}&offset=0" target="_self">|<首页 </a> """
		appendLink(next," 下一页>>")  //<a href="/inspector/menu/show/${params.id}?max=${max}&offset=0" target="_self">|<首页 </a> """
		appendLink((page < pageCount ? end : -1),"末页>|")  //<a href="/inspector/menu/show/${params.id}?max=${max}&offset=0" target="_self">|<首页 </a> """
        
		out <<  """ <script type="text/javascript">
											function goToPage(){
												var p = null
												try{
										   			try{
														p = parseInt(document.getElementById('pageNo').value)
													}
													catch(e){
							                                  throw '录入正确的页数'
													}
							                        var max = ${max}
													var pageCount = ${pageCount}
							
													if(p < 1){
														throw "页数不能<1"
													}
							
													if(p > pageCount){
														throw "最多只有${pageCount}页"
													}
							
							                        var offset = max * (p-1)
													window.location.href = "/inspector/menu/show/${params.id}?max=${max}&offset=" + offset
												}catch(e){
												    alert(e)
												}
											}
		</script>
			<label>页次：${page}/${pageCount}页</label>
			<label> 共有 ${total} 条</label>
            <label>转到</label>
			<input maxlength="5" size="1" id="pageNo" value="${page}" type="TEXT">页
			<input type="button" onclick ="javascript:goToPage()" class="b2" value="GO" style="font-size: 9pt">
			</div>
		"""
		  
		
	}
}
